package com.hp.framework.utils;

import java.util.Collection;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;

/**
 * 分页
 * 默认从0行开始计算,最小页数为1
 *
 *
 */
public class Page {
	public static final int DEFAULT_ROWS = 1;
	public static final int DEFAULT_PAGE = 1;
	
	private long totalCount;
	private int pageNo;
	private int pageSize;
	private String url;
	private String parames;
	private Collection<?> data;
	
	public Page(long totalCount, int pageNo, int pageSize) {
		this(totalCount, pageNo, pageSize, null, null);
	}
	
	public Page(long totalCount, int pageNo, int pageSize, String url, String parames) {
		init(totalCount, pageNo, pageSize, url, parames);
	}
	
	private void init(long totalCount, int pageNo, int pageSize, String url, String parames) {
		this.setTotalCount(totalCount);
		this.setPageSize(pageSize);
		this.setPageNo(pageNo);
		this.setUrl(url);
		this.setParames(parames);
	}
	
	public long getStartRows() {
		int val = (getPageNo() - 1) * getPageSize();
		if (val < 0) {
			return 0;
		}
		if (val > this.getTotalCount()) {
			return (this.getTotalPages() - 1) * this.getPageSize();
		}
		return val;
	}
	
	public long getEndRows() {
		long val = getStartRows() + this.getPageSize();
		if (val > this.getTotalCount()) {
			val = this.getTotalCount();
		}
		
		return val;
	}
	
	public long getTotalPages() {
		if (getTotalCount() <= 0) {
			return Page.DEFAULT_PAGE;
		}
		int count = (int)getTotalCount() / getPageSize();
		if (getTotalCount() % getPageSize() > 0) {
			count++;
		}
		return count;
	}
	
	public boolean isHasNext() {
		return (getPageNo() + 1 <= getTotalPages());
	}
	
	//获取下一页
	public int getNextPage() {
		if (isHasNext()) {
			return getPageNo() + 1;
		} else {
			return getPageNo();
		}
	}
	//是否有上一页
	public boolean isHasPre() {
		return (getPageNo() - 1 >= DEFAULT_PAGE);
	}
	//获取上一页
	public int getPrePage() {
		if (isHasPre()) {
			return getPageNo() - 1;
		} else {
			return getPageNo();
		}
	}
	
	public long getTotalCount() {
		return totalCount;
	}

	
	//------------------------------
	
	public void setTotalCount(long totalCount) {
		this.totalCount = totalCount;
		if (this.totalCount < 0) {
			this.totalCount = 0;
		}
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
		if (getPageNo() > this.getTotalPages()) {
			this.pageNo = (int)this.getTotalPages();
		}
		if (getPageNo() < 1) {
			this.pageNo = 1;
		}
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getParames() {
		if (StringUtils.isEmpty(parames)) {
			return StringUtils.EMPTY;
		}
		return parames;
	}

	public void setParames(String parames) {
		this.parames = parames;
	}

	public Collection<?> getData() {
		return data;
	}

	public void setData(Collection<?> data) {
		this.data = data;
	}
	
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
}
